/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.console.web;
import com.credithc.eagleeye.console.service.SettingService;
import com.credithc.eagleeye.console.vo.PlatformVo;
import com.credithc.eagleeye.console.vo.SettingVo;
import com.credithc.eagleeye.console.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
 * <br>类 名: AbstractAction
 * <br>描 述: 描述类完成的主要功能
 * <br>作 者: zj
 * <br>创 建： 2015年12月14日
 * <br>版 本：v1.0.0
 * <br>
 * <br>历 史: (版本) 作者 时间 注释
 */
public abstract class AbstractAction {

    @Autowired
    private SettingService settingService;


    protected abstract String getFlag();
	protected abstract String getTitle();

	public static final String TITLE = "title";

    protected void putPublicAttribute(ModelMap map) {
        map.put(getFlag(), "active");
        map.put(TITLE, getTitle());
    }

    protected void putPlatformListAttribute(HttpServletRequest request,ModelMap map){
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("user");
        SettingVo settingVo = settingService.loadSetting();
        List<PlatformVo> allPlatformVoList = this.settingService.loadAllPlatform();
        if(!StringUtils.equals(userName,"admin")){
            List<PlatformVo> platformVoList = new ArrayList<>();
            UserVo userVo = settingService.loadUser(userName);
            List<String> platformList = userVo.getPlatformList();
            for(PlatformVo platformVo:allPlatformVoList){
                if(platformList.contains(platformVo.getName())){
                    platformVoList.add(platformVo);
                }
            }
            map.put("platformList",platformVoList );
        }else {
            PlatformVo platformVo = new PlatformVo();
            platformVo.setName("");
            platformVo.setDescribe("全部");
            allPlatformVoList.add(platformVo);
            map.put("platformList",allPlatformVoList );
        }
        map.put("envList", settingVo.getEnvList());
        map.put("resultList", settingVo.getResultList());
        map.put("rpcTypeList", settingVo.getRpcTypeList());
        map.put("userInfoTypeList", settingVo.getUserInfoTypeList());
    }
}
